﻿using System;
using System.Text;
using System.Xml;
using IAM.Interface.Model;
using IAM.Interface.Model.ENUM;
using IAM.Interface.Common.Abnormity;

namespace IAM.Interface.Common.XMLResolution
{
    /// <summary>
    /// xml响应包
    /// </summary>
    public class Response : AbstractXMLResolution
    {
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="actintype">操作类型</param>
        public Response(ActionType actintype)
        {
            _actintype = actintype;
            
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="actintype">操作类型</param>
        /// <param name="exception">异常消息</param>
        public Response(ActionType actintype, ResponseException exception)
        {
            _actintype = actintype;
            _exception = exception;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="actintype">操作类型</param>
        /// <param name="transactionid">发起请求的事务ID</param>
        public Response(ActionType actintype, string transactionid)
        {
            _actintype = actintype;
            _transactionid = transactionid;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="actintype">操作类型</param>
        /// <param name="exception">异常消息</param>
        /// <param name="transactionid">发起请求的事务</param>
        public Response(ActionType actintype, ResponseException exception, string transactionid)
        {
            _actintype = actintype;
            _exception = exception;
            _transactionid = transactionid;
        }
        #endregion
        #region 属性
        private ActionType _actintype = ActionType.Nullity;
        private ResponseException _exception = null;
        private string _transactionid = "";
        /// <summary>
        /// 请求包头
        /// </summary>
        public ActionType ActionType
        {
            get
            {
                return _actintype;
            }
            set
            {
                _actintype = value;
            }
        }
        /// <summary>
        /// 异常消息
        /// </summary>
        public ResponseException Exception
        {
            get
            {
                return _exception;
            }
            set
            {
                _exception = value;
            }
        }
        /// <summary>
        /// 发起请求的事务ID
        /// </summary>
        public string TransactionID
        {
            get
            {
                return _transactionid;
            }
            set
            {
                _transactionid = value;
            }
        }
        public Result Result
        {
            get
            {
                if (Exception == null)
                {
                    throw (new System.Exception("No setting Exception")); ;
                }
                else
                {
                    IAM.Interface.Model.Result result = new IAM.Interface.Model.Result();
                    result.ErrorCode = System.Convert.ToString(Exception.StatusCode);
                    result.ErrorMessage = Exception.Message;
                    return result;
                }
            }
        }
        #endregion
        #region 获取当前结点
        /// <summary>
        /// 获取当前结点
        /// </summary>
        /// <returns></returns>
        public override XmlNode GetCurrentNode()
        {
            XmlElement xe = XmlDocument.DocumentElement;
            XmlNode xn = null;
            //string strActionType = xe.GetAttribute("ActionType");
            switch (ActionType)
            {
                case ActionType.IDEN_MANAGE://身份信息管理
                    xn = xe.SelectSingleNode("AccountInfo");
                    break;
                case ActionType.IDEN_GETACMODEL://获取权限模型
                    xn = xe.SelectSingleNode("ACMMode");
                    break;
                case ActionType.IDEN_GETUSERACI://获取用户权限信息
                    xn = (XmlNode)xe;
                    break;
                case ActionType.IDEN_SETUSERACI://设置用户权限信息
                    xn = (XmlNode)xe;
                    break;
                case ActionType.ORG_UPDATE://组织信息更新
                    xn = (XmlNode)xe;
                    break;
                case ActionType.ORG_CREATE://组织机构创立
                    xn = (XmlNode)xe;
                    break;
                case ActionType.ORG_DELETE://组织结构删除（包括合并）
                    xn = (XmlNode)xe;
                    break;
                case ActionType.DM_GETMODULELIST://获取可代理模块列表
                    xn = xe.SelectSingleNode("Modules");
                    break;
                case ActionType.DM_GETDELEINFO://获取用户代理信息
                    xn = (XmlNode)xe;
                    break;
                case ActionType.DM_SETDELEINFO://设置用户代理信息
                    xn = (XmlNode)xe;
                    break;
                //case "IDEN_USERLIST"://获取用户信息列表
                //    xn = (XmlNode)xe;
                //    break;
                //case "AUDIT_FETCH"://获取审计信息
                //    xn = xe.SelectSingleNode("Logs");
                //    break;
            }

            return xn;
        }
        #endregion
        #region 拼完整的信息包
        /// <summary>
        /// 拼完整的信息包
        /// 1.实例化ResponseData 对象
        /// 2.为ResponseData 对象赋值
        /// 3.拼写完整xml响应信息包
        /// </summary>
        /// <returns></returns>
        public override string GetWholeXML()
        {
            //1.实例化ResponseData 对象
            ResponseData responsedata = new ResponseData();

            //2.为ResponseData 对象赋值
            responsedata.TransactionID = TransactionID;
            responsedata.ResponseTime = System.DateTime.Now;
            responsedata.StatusCode = Exception.StatusCode;
            if (Exception.StatusCode == 200)
            {
                responsedata.ReturnMessage = "OK";
            }
            else
            {
                responsedata.ReturnMessage = "Error";
            }
            //3.拼写完整xml响应信息包
            StringBuilder xml = new StringBuilder();
            xml.Append(responsedata.XMLBoday);
            xml.Append(Result.XML);
            if (Exception != null && Exception.StatusCode == 200)//正确处理时,加载处理后的信息
            {
                xml.Append(XmlInfo);
            }
            xml.Append(responsedata.XMLFinis);
            return xml.ToString(); ;
        }
        #endregion
    }
}
